package com.syj.jsxjj.base.tab;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.AttributeSet;

/**
 * Created by gyl on 2017/1/12 0012.
 */

public class FragmentHostTabGroup extends FragmentTabGroup {

    public FragmentHostTabGroup(Context activity, int layoutId) {
        super(activity, layoutId);
    }

    public FragmentHostTabGroup(Context activity, AttributeSet attrs) {
        super(activity, attrs);
    }

    @Override
    public void addTab(Class<?> fragmentClass, Bundle args) {
        TabInfo tabInfo = new TabInfo(fragmentClass, args);
        tabInfo.tag = getContainerId() + ":" + mTabs.size() + ":" + fragmentClass.hashCode();
        //检查是否有这个标签页面处理
        Fragment fragment = getFragmentManager().findFragmentByTag(tabInfo.tag);
        if (fragment != null && !fragment.isDetached()) {
            getFragmentManager().beginTransaction().detach(fragment).commit();
        }
        mTabs.add(tabInfo);
    }

    @Override
    protected void onTabSelected(int position) {
        if (position < 0 || position >= mTabs.size()) {
            throw new IllegalArgumentException("position out of bounds");
        }
        if (position != mCurrentPosition) {
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            // 选择最后
            TabInfo lastTab = (mCurrentPosition == -1) ? null : mTabs.get(mCurrentPosition);
            Fragment lastFragment = (mCurrentPosition == -1) ? null : getFragmentByTag(lastTab.tag);
            if (lastTab != null && lastFragment != null) {
                lastFragment.setMenuVisibility(false);
                lastFragment.setUserVisibleHint(false);
                transaction.detach(lastFragment);
            }
            // 选择
            TabInfo newTab = mTabs.get(position);
            Fragment newFragment = getFragmentByTag(newTab.tag);
            if (newTab != null) {
                if (newFragment == null) {
                    newFragment = Fragment.instantiate(getContext(), newTab.fragmentClass.getName(), newTab.args);
                    transaction.add(getContainerId(), newFragment, newTab.tag);
                } else {
                    newFragment.setMenuVisibility(true);
                    newFragment.setUserVisibleHint(true);
                    transaction.attach(newFragment);
                }
            }
            mCurrentPosition = position;
            transaction.commitAllowingStateLoss();
            getFragmentManager().executePendingTransactions();
        }

        if (mTabChangedListener != null) {
            mTabChangedListener.onTabChanged(position);
        }

        Fragment fragment = getFragmentByTag(getFragmentTag(position));
        if (fragment != null && fragment instanceof TabChangedListener) {
            TabChangedListener listener = (TabChangedListener) fragment;
            listener.onTabChanged(position);
        }
    }

}
